package com.aletto.demo_android_web3j;

import android.os.Bundle;

import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView button_approve = findViewById(R.id.button_approve);
        TextView button_allowance = findViewById(R.id.button_allowance);
        TextView button_balance_1 = findViewById(R.id.button_balance_1);
        TextView button_balance_2 = findViewById(R.id.button_balance_2);

        button_approve.setOnClickListener(v -> new Thread(() -> ExampleTest.approve(this::console)).start());
        button_allowance.setOnClickListener(v -> new Thread(() -> ExampleTest.allowance(this::console)).start());
        button_balance_1.setOnClickListener(v -> new Thread(() -> ExampleTest.balance_1(this::console)).start());
        button_balance_2.setOnClickListener(v -> ExampleTest.balance_2(this::console));


        TextView button_buy = findViewById(R.id.button_buy);
        TextView button_take = findViewById(R.id.button_take);
        TextView button_refund = findViewById(R.id.button_refund);
        TextView button_raise_arbitration = findViewById(R.id.button_raise_arbitration);

        button_buy.setOnClickListener(v -> new Thread(() -> MarketTestDemo.buy(this::console)).start()); // 购买
        button_take.setOnClickListener(v -> new Thread(() -> MarketTestDemo.resolveArbitration(this::console)).start()); // 商家取回货款
        button_refund.setOnClickListener(v -> new Thread(() -> MarketTestDemo.refund(this::console)).start()); // 商家退款
        button_raise_arbitration.setOnClickListener(v -> new Thread(() -> MarketTestDemo.raiseArbitration(this::console)).start()); // 买家申请仲裁
    }


    private void console(String string) {
        runOnUiThread(() -> {
            TextView console = findViewById(R.id.text_console);
            console.setText(
                    new StringBuilder(console.getText())
                            .append(string)
                            .append("\n=================================\n")
            );
        });
    }

}
